#!/usr/bin/env bash

REPO_SERVER=192.168.20.160
TOOLS_DIR=/home/lavenliu/tools
APP_DIR=/application
SCRIPT_DIR=/server/scripts

[ ! -d ${TOOLS_DIR} ] && mkdir -p ${TOOLS_DIR}
[ ! -d ${APP_DIR} ] && mkdir -p ${APP_DIR}
[ ! -d ${SCRIPT_DIR} ] && mkdir -p ${SCRIPT_DIR}

### 不使用系统默认的yum源，使用阿里云的yum源
/bin/mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
/bin/mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
wget -O /etc/yum.repos.d/lan.repo http://${REPO_SERVER}/conf/lan.repo

### 禁用不需要的服务开机自启动
## 先关闭所有的开机自启动的服务
for service in `chkconfig --list |grep 3:on |awk '{ print $1 }'` ;
do
    chkconfig --level 3 ${service} off;
done

## 开启需要开机自启动的服务
for service in rsyslog sshd network crond; do
    chkconfig --level 3 ${service} on
done

## 一行搞定的写法
## 鉴于很多人看不懂，注释了改行
# chkconfig --list |grep 3:on |egrep -v "sshd|rsyslog|crond|network|jenkins" |awk '{ print $1 }' |sed -r 's#(.*)#chkconfig \1 off#g'

### 设置时间同步
## 如果不能访问互联网，需要使用内部NTP服务
## 这里假设可以访问互联网
echo "### added by lavenliu for time sync"
echo "*/5 * * * * /usr/sbin/ntpdate time.nist.gov > /dev/null 2>&1" >> /var/spool/cron/root
crontab -l

### 修改最大文件打开数
## 修改完毕，要重启系统
echo "### added by lavenliu for opening max files"
echo "*                -       nofile          65535" >> /etc/security/limits.conf

### 关闭selinux
## 关闭之后，要重启系统
# sed -i 's#^SELINUX=enforcing#SELINUX=disabled#' /etc/sysconfig/selinux

### 内核参数优化
cat >> /etc/sysctl.conf <<EOF
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.ip_local_port_range = 4000 65000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_max_tw_buckets = 360000
net.ipv4.route.gc_timeout = 100
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
EOF
sysctl -p

### 隐藏Linux内核版本
> /etc/issue

